package de.uni.muenster.idw2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import de.uni.muenster.idw2.data.BBBuilding;
import de.uni.muenster.idw2.data.Building;
import de.uni.muenster.idw2.interfaces.BuildingMapInterface;
import de.uni.muenster.idw2.threads.BuildingLoaderThread;

public class BuildingMap extends Activity implements BuildingMapInterface{
	
	private Building building = null;
	private BuildingLoaderThread buildingLoader;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.building_view_map);
		
		
		/**
		 * get The building xml
		 */
		BBBuilding bbBuilding = (BBBuilding) getIntent().getExtras().getSerializable(BuildingChooserMap.MAP_ACTIVITY_RESULT_DATA);
		buildingLoader = new BuildingLoaderThread(bbBuilding, this);
		buildingLoader.execute();
		
		
		
	}
	
    public void info(View view){
    	if(building != null){
			Intent intent = new Intent(this, BuildingInformation.class);
	    	intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
	    	startActivity(intent);
			}
			else{
				Toast.makeText(this, "NO USEFUL BUILDING INFORMATION", 4).show();
			}
    }
    
    public void navigate(View view){
    	if(building != null){
    		Intent intent = new Intent(this, RouteSelector.class);
        	intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        	startActivity(intent);
    		}
    		else{
    			Toast.makeText(this, "NO USEFUL BUILDING INFORMATION", 4).show();
    		}
    }
	
	@Override
	public void loadMap(Building building) {
		this.building = building;
		Log.d("DEBUG", building.toString());
		
		com.indoornavi.verbal.graph.Graph g = new com.indoornavi.verbal.graph.Graph("http://checkout.yourweb.de/graph.dat"); 
		com.indoornavi.verbal.graph.Path pth = g.Dijkstra("R0.01", "R0.06");
		setContentView(new MapView(this, building, pth));
		
		//setContentView(new MapView(this, building));
		
		//TODO: load the building map...
		
		//could look like this
		//LocationController.startAllLocationUpdates(building);
		// mapview.enableReceiver
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		if (buildingLoader != null && !buildingLoader.isCancelled()) {
			buildingLoader.cancel(true);
		}
	}
	
}
